package cn.edu.zzu.demo01Thread;

/**
 * 线程停止
 *
 */
public class TestStopThread03 implements Runnable{

    private  Boolean flag = true; //标志位

    public static void main(String[] args) {
        TestStopThread03 testStopThread = new TestStopThread03();
        //子线程
        new Thread(testStopThread).start();
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName()+" 执行了 "+i);

            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            //主线程执行了90次，子线程该结束了 ， 此时仅仅把主线程后续的执行完毕即可
            if (i==90){
                testStopThread.stop();
            }
        }
    }


    @Override
    public void run() {
        int  i = 0;
        while (flag){
            System.out.println(Thread.currentThread().getName()+" 执行了"+i++);
        }
        System.out.println(Thread.currentThread().getName()+" 线程结束了！");
    }

    /**
     * 线程停止方法
     */
    public void stop(){
        flag = false;
    }
}

